package lainey.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.Window;
import android.widget.TextView;

import lainey.ui.R;

public class LoadingDialog extends Dialog {

    private TextView mTvMessage;
    private String mHintText;

    public LoadingDialog(Context context, String text) {
        this(context, R.style.UI_DefaultDialogStyle, text);
    }

    private LoadingDialog(Context context, int theme, String text) {
        super(context, theme);
        init(text);
    }

    private void init(String msg) {
        setContentView(R.layout.ui_dialog_loading);
        Window window = getWindow();
        if (window != null) {
            // 居中
            window.getAttributes().gravity = Gravity.CENTER;
        }
        // 点击空白不取消
        setCanceledOnTouchOutside(false);
        // 点击返回按钮不取消
        setCancelable(false);

        mTvMessage = findViewById(R.id.tv_message);
        setMessage(msg);
    }

    public void setMessage(String msg) {
        mHintText = msg;
        mHintText = TextUtils.isEmpty(mHintText) ? getContext().getString(R.string.ui_loading) : mHintText;
        mTvMessage.setText(mHintText);
    }

    public void updateProgress(int progress) {
        String format = String.format("%s(%ds)", mHintText, progress);
        mTvMessage.setText(format);
    }

}
